package com.handydata.antifraud.service.impl;

import com.handydata.antifraud.bo.rejectprovincebasedata.RejectProvinceBasedataBeanBO;
import com.handydata.antifraud.bo.rejectprovincebasedata.RejectProvinceBasedataReqBO;
import com.handydata.antifraud.bo.rejectprovincebasedata.RejectProvinceBasedataResBO;
import com.handydata.antifraud.common.exception.RejectProvinceBasedataExCode;
import com.handydata.antifraud.dao.RejectProvinceBasedataDao;
import com.handydata.antifraud.entity.RejectProvinceBasedataDO;
import com.handydata.antifraud.service.RejectProvinceBasedataService;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.exception.SysException;
import com.handydata.common.service.AbsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("rejectProvinceBasedataService")
public class RejectProvinceBasedataServiceImpl extends AbsService implements RejectProvinceBasedataService {
	
	private static final Logger logger = LoggerFactory.getLogger(RejectProvinceBasedataServiceImpl.class);
	
	@Autowired
	private RejectProvinceBasedataDao rejectProvinceBasedataDao;

	@Transactional
	@Override
	public RejectProvinceBasedataResBO createRejectProvinceBasedata(RejectProvinceBasedataReqBO req) throws SysException{
	    RejectProvinceBasedataResBO res = new RejectProvinceBasedataResBO();
		try {
			RejectProvinceBasedataDO entity = CommonBeanConvertor.convert(req, RejectProvinceBasedataDO.class);
			long status = rejectProvinceBasedataDao.insert(entity);
			if(status == 0) {
				res.setExCode(RejectProvinceBasedataExCode.SYS_NO_ENTITY_CREATED);
			} else {
				res.setExCode(RejectProvinceBasedataExCode.SUCCESS);
			}
 			
		} catch (Exception e) {
			logger.error("Create new entity {} get into error", req, e);
			throw new SysException(RejectProvinceBasedataExCode.SYS_CREATE_ENTITY_ERROR);
		}
		
		return res;
	}

	@Transactional
	@Override
	public RejectProvinceBasedataResBO modifyRejectProvinceBasedata(RejectProvinceBasedataReqBO req) throws SysException{
	    RejectProvinceBasedataResBO res = new RejectProvinceBasedataResBO();
		try {
			RejectProvinceBasedataDO condition = CommonBeanConvertor.convert(req, RejectProvinceBasedataDO.class);
			long status = rejectProvinceBasedataDao.update(condition);
			if(status == 0) {
				res.setExCode(RejectProvinceBasedataExCode.SYS_NO_ENTITY_MODIFIED);
			} else {
				res.setExCode(RejectProvinceBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Modify entity {} get into error", req, e);
			throw new SysException(RejectProvinceBasedataExCode.SYS_MODIFY_ENTITY_ERROR);
		} 
		return res;
	}

	@Transactional
	@Override
	public RejectProvinceBasedataResBO deleteRejectProvinceBasedata(RejectProvinceBasedataReqBO req) throws SysException{
	    RejectProvinceBasedataResBO res = new RejectProvinceBasedataResBO();
		
		try {
			RejectProvinceBasedataDO condition = CommonBeanConvertor.convert(req, RejectProvinceBasedataDO.class);
			long status = rejectProvinceBasedataDao.delete(condition);
			if(status == 0) {
				res.setExCode(RejectProvinceBasedataExCode.SYS_NO_ENTITY_DELETED);
			} else {
				res.setExCode(RejectProvinceBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Delete entity {} get into error", req, e);
			throw new SysException(RejectProvinceBasedataExCode.SYS_DELETE_ENTITY_ERROR);
		}
		return res;
	}

	@Override
	public RejectProvinceBasedataResBO queryRejectProvinceBasedata(RejectProvinceBasedataReqBO req) throws SysException{
		RejectProvinceBasedataDO condition = CommonBeanConvertor.convert(req, RejectProvinceBasedataDO.class);
		RejectProvinceBasedataResBO res;
		try {
			List<RejectProvinceBasedataDO> entities = rejectProvinceBasedataDao.query(condition);
			res = CommonBeanConvertor.convert(entities, RejectProvinceBasedataResBO.class, RejectProvinceBasedataBeanBO.class);
		} catch(Exception e) {
			logger.error("Query entity {} get into error", req, e);
			throw new SysException(RejectProvinceBasedataExCode.SYS_QUERY_ENTITY_ERROR);
		}
		return res;
	}
	
}
